package cn.northsea.controller;


import cn.northsea.model.buy.Buy;
import cn.northsea.model.logistics.Logistics;
import cn.northsea.model.material.Material;
import cn.northsea.model.material.Material_detail;
import cn.northsea.model.supplier.Supplier;
import cn.northsea.model.user.UserInfo;
import cn.northsea.model.warehouse.Warehouse;
import cn.northsea.pojo.Result;
import cn.northsea.service.LogisticsService;
import cn.northsea.vo.logistics.LogisticsQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author abc
 * @since 2024-08-21
 */
@RestController
@RequestMapping("/logistics")
@CrossOrigin
public class LogisticsController {

    @Autowired
    private LogisticsService logisticsService;

    @ApiOperation(value = "获取分页列表")
    @PostMapping("{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true, example = "1")
            @PathVariable("page") Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true, example = "1")
            @PathVariable("limit") Long limit,
            @ApiParam(name = "logisticsQueryVo", value = "查询对象", required = false)
            @RequestBody(required = false) LogisticsQueryVo logisticsQueryVo) {
        Page<Logistics> pageParam = new Page<>(page, limit);
        Map<String, Object> map = logisticsService.findPage(pageParam, logisticsQueryVo);
        return Result.ok(map);
    }

    @GetMapping("/getLogistics/{id}")
    public Result getLogistics(@PathVariable String id) {
        Logistics logistics = logisticsService.getById(id);
        return Result.ok(logistics);
    }

    @PostMapping("/addLogistics")
    public Result addLogistics(@RequestBody Logistics logistics) {
        logisticsService.save(logistics);
        return Result.ok();
    }

    @PutMapping("/updateLogistics")
    public Result updateLogistics(@RequestBody Logistics logistics) {
        logisticsService.updateById(logistics);
        return Result.ok();
    }

    @DeleteMapping("/deleteLogistics/{id}")
    public Result deleteLogistics(@PathVariable String id) {
        logisticsService.removeById(id);
        return Result.ok();
    }

    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        logisticsService.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation(value = "导出")
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {
        logisticsService.exportData(response);
    }

    @ApiOperation(value = "导入")
    @PostMapping("importData")
    public Result importData(MultipartFile file) {
        logisticsService.importData(file);
        return Result.ok();
    }

    @PutMapping("updateState/{id}/{state}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer state) {
        logisticsService.updateState(id, state);
        return Result.ok();
    }


    @PostMapping("getBuyByLogisticsId/{id}")
    public Result getBuyListByLogisticsId(@PathVariable Long id) {
        Buy logistics = logisticsService.getBuyByLogisticsId(id);
        return Result.ok(logistics);
    }

    @PostMapping("getLogisticsListByUserId/{id}")
    public Result getLogisticsListByUserId(@PathVariable Long id) {
        List<Logistics> logisticsList = logisticsService.getLogisticsListByUserId(id);
        return Result.ok(logisticsList);
    }


    @PostMapping("getMaterialByBuyId/{id}")
    public Result getMaterialByBuyId(@PathVariable Long id) {
        Material material = logisticsService.getMaterialByBuyId(id);
        return Result.ok(material);
    }

    @PostMapping("getSupplierByBuyId/{id}")
    public Result getSupplierByBuyId(@PathVariable Long id) {
        Supplier supplier = logisticsService.getSupplierByBuyId(id);
        return Result.ok(supplier);
    }

    @PostMapping("getMaterial_detailByMaterialId/{id}")
    public Result getMaterial_detailByMaterialId(@PathVariable Long id) {
        Material_detail material_detail = logisticsService.getMaterial_detailByMaterialId(id);
        return Result.ok(material_detail);
    }


    @PostMapping("getWarehouseByMaterial_detailId/{id}")
    public Result getWarehouseByMaterial_detailId(@PathVariable Long id) {
        Warehouse warehouse = logisticsService.getWarehouseByMaterial_detailId(id);
        return Result.ok(warehouse);
    }

    @PostMapping("getUserById/{id}")
    public Result getUserById(@PathVariable Long id) {
        UserInfo userInfo = logisticsService.getUserById(id);
        return Result.ok(userInfo);
    }

    @PostMapping("addMaterialToWarehouse/{id}/{number}")
    public Result addMaterialToWarehouse(@PathVariable Long id,@PathVariable Integer number) {
        logisticsService.addMaterialToWarehouse(id,number);
        return Result.ok();
    }
}

